package com.qiguliuxing.dts.merchant.controller;

import com.qiguliuxing.dts.db.bean.GoodsAllinone;
import com.qiguliuxing.dts.merchant.annotation.RequiresPermissionsDesc;
import com.qiguliuxing.dts.common.constant.ApiAdminConsts;
import com.qiguliuxing.dts.common.constant.LogParamsConsts;
import com.qiguliuxing.dts.core.annotation.aoplog.WebLog;
import com.qiguliuxing.dts.core.validator.Order;
import com.qiguliuxing.dts.core.validator.Sort;
import com.qiguliuxing.dts.db.domain.DtsGoods;
import com.qiguliuxing.dts.merchant.service.MerchantGoodsService;
import com.qiguliuxing.dts.merchant.support.AuthSupport;
import com.qiguliuxing.dts.merchant.support.MerchantResponseCode;
import com.qiguliuxing.dts.merchant.support.MerchantResponseUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;

/**
 * 商户商品管理接口
 * @author qiguliuxing
 * @since 1.0.0
 */
@RestController
@RequestMapping("/merchant/goods")
@Validated
@Api(tags = ApiAdminConsts.Goods.PARENT)
public class MerchantGoodsController {
	private static final Logger logger = LoggerFactory.getLogger(MerchantGoodsController.class);

	@Autowired
	private MerchantGoodsService merchantGoodsService;

	/**
	 * 查询商品
	 *
	 * @param goodsSn
	 * @param name
	 * @param page
	 * @param limit
	 * @param sort
	 * @param order
	 * @return
	 */
	@GetMapping("/list")
	@RequiresPermissions("merchant:goods:list")
	@RequiresPermissionsDesc(menu = { "商品管理", "商品管理" }, button = "查询")
	@ApiOperation(value=ApiAdminConsts.Goods.LIST)
	@WebLog(description = ApiAdminConsts.Goods.LIST,excludes = {LogParamsConsts.RESPONSE})
	public Object list(@ApiParam(value = "商品编号") String goodsSn,
					   @ApiParam(value = "商品名称") String name,
					   @ApiParam(value = "审批状态") @RequestParam(required = false) List<Byte> approveStatusArray,
					   @ApiParam(value = "页码",defaultValue = "1")  @RequestParam(defaultValue = "1") Integer page,
					   @ApiParam(value = "分页大小",defaultValue = "10")  @RequestParam(defaultValue = "10") Integer limit,
					   @ApiParam(value = "排序字段",defaultValue = "add_time")  @Sort @RequestParam(defaultValue = "add_time") String sort,
					   @ApiParam(value = "排序规则",defaultValue = "desc")  @Order @RequestParam(defaultValue = "desc") String order) {
	    //需要区分数据权限，如果属于品牌商管理员，则需要获取当前用户管理品牌店铺
		List<Integer> brandIds = new ArrayList<Integer> ();
		brandIds.add(AuthSupport.merchantBrandId());
		return merchantGoodsService.list(goodsSn, name, approveStatusArray, page, limit, sort, order, brandIds);
	}

	@ApiOperation(value="catAndBrand")
	@GetMapping("/catAndBrand")
	public Object catAndBrand() {
		return merchantGoodsService.catAndBrand();
	}

	/**
	 * 编辑商品
	 *
	 * @param goodsAllinone
	 * @return
	 */
	@PostMapping("/update")
	@RequiresPermissions("merchant:goods:update")
	@RequiresPermissionsDesc(menu = { "商品管理", "商品管理" }, button = "编辑")
	@ApiOperation(value=ApiAdminConsts.Goods.UPDATE)
	@WebLog(description = ApiAdminConsts.Goods.UPDATE,persistence = true)
	public Object update(@RequestBody GoodsAllinone goodsAllinone) {
		if (goodsAllinone.getGoods().getBrandId().intValue() != AuthSupport.merchantBrandId().intValue()) {
			logger.error("更新商品失败：{}", MerchantResponseCode.INVALID_CURR_MERCHANT.desc());
			return MerchantResponseUtils.fail(MerchantResponseCode.INVALID_CURR_MERCHANT);
		}
		return merchantGoodsService.update(goodsAllinone);
	}

	/**
	 * 删除商品
	 *
	 * @param goods
	 * @return
	 */
	@PostMapping("/delete")
	@RequiresPermissions("merchant:goods:delete")
	@RequiresPermissionsDesc(menu = { "商品管理", "商品管理" }, button = "删除")
	@ApiOperation(value=ApiAdminConsts.Goods.DELETE)
	@WebLog(description = ApiAdminConsts.Goods.DELETE,persistence = true)
	public Object delete(@RequestBody DtsGoods goods) {
		if (goods.getBrandId().intValue() != AuthSupport.merchantBrandId().intValue()) {
			logger.error("更新商品失败：{}", MerchantResponseCode.INVALID_CURR_MERCHANT.desc());
			return MerchantResponseUtils.fail(MerchantResponseCode.INVALID_CURR_MERCHANT);
		}
		return merchantGoodsService.delete(goods);
	}

	/**
	 * 添加商品
	 *
	 * @param goodsAllinone
	 * @return
	 */
	@PostMapping("/create")
	@RequiresPermissions("merchant:goods:create")
	@RequiresPermissionsDesc(menu = { "商品管理", "商品管理" }, button = "上架")
	@ApiOperation(value=ApiAdminConsts.Goods.CREATE)
	@WebLog(description = ApiAdminConsts.Goods.CREATE,persistence = true)
	public Object create(@RequestBody GoodsAllinone goodsAllinone) {
		return merchantGoodsService.create(goodsAllinone);
	}

	/**
	 * 商品详情
	 *
	 * @param id
	 * @return
	 */
	@GetMapping("/detail")
	@RequiresPermissions("merchant:goods:read")
	@RequiresPermissionsDesc(menu = { "商品管理", "商品管理" }, button = "详情")
	@ApiOperation(value=ApiAdminConsts.Goods.READ)
	@WebLog(description = ApiAdminConsts.Goods.READ)
	public Object detail(@ApiParam(value = "商品id") @NotNull Integer id) {
		return merchantGoodsService.detail(id);
	}

}
